﻿@using System.Globalization
@using CkSoftware.GroupMe.Sdk.TestWeb.Helpers
@using CkSoftware.GroupMe.Sdk.TestWeb.Models
@using TwitterBootstrapMVC.Controls
@model GroupDetailModel

@{
    ViewBag.Title = "Details of the group '" + Model.Data.Data.Name + "'";
}

<div class="page-header">
    <h1>Details of the group '@Model.Data.Data.Name'</h1>
</div>

@if (!SessionConfiguration.IsConfigValueIsSet<string>(SessionConfiguration.GroupMeClientAuthKey))
{
    <h2>Configuration required</h2>
    <p>Before you can start here, you have to configure your GroupMe Access Token @Html.Bootstrap().ActionLink("here", "Index").</p>
}
else
{
    <h2>Group Details</h2>
    <dl class="dl-horizontal">
        <dt>Id</dt>
        <dd>@Html.TextOrDash(Model.Data.Data.Id)</dd>
        <dt>Name</dt>
        <dd>@Html.TextOrDash(Model.Data.Data.Name)</dd>
        <dt>Created</dt>
        <dd>@Html.TextOrDash(Model.Data.Data.CreatedAt.ToString(CultureInfo.CurrentCulture))</dd>
        <dt>Creator user ID</dt>
        <dd>@Html.TextOrDash(Model.Data.Data.CreatorUserId)</dd>
        <dt>Office mode</dt>
        <dd>@Html.TextOrDash(Model.Data.Data.OfficeMode.ToString())</dd>
        <dt>Last group update</dt>
        <dd>@Html.TextOrDash(Model.Data.Data.UpdatedAt.ToString(CultureInfo.CurrentCulture))</dd>
        <dt>Description</dt>
        <dd>@Html.TextOrDash(Model.Data.Data.Description)</dd>
        <dt>Messages count</dt>
        <dd>@Html.TextOrDash(Model.Data.Data.Messages.Count.ToString(CultureInfo.CurrentCulture))</dd>
        <dt>Last message ID</dt>
        <dd>@Html.TextOrDash(Model.Data.Data.Messages.LastMessageId)</dd>
        <dt>Last msg created at</dt>
        <dd>@Html.TextOrDash(Model.Data.Data.Messages.LastMessageCreatedAt.ToString(CultureInfo.CurrentCulture))</dd>
        <dt>Preview nickname</dt>
        <dd>@Html.TextOrDash(Model.Data.Data.Messages.Preview.Nickname)</dd>
        <dt>Preview text</dt>
        <dd>@Html.TextOrDash(Model.Data.Data.Messages.Preview.Text)</dd>
        <dt>Share URL</dt>
        <dd>@Html.TextOrDash(Model.Data.Data.ShareUrl)</dd>
        <dt>Messages</dt>
        <dd>@Html.ActionLink("View all messages of this group", "GroupMessages", new { groupId = Model.GroupId })</dd>
    </dl>
    
    <h2>Group members</h2>
    
    using(var memberTable = Html.Bootstrap().Begin(new Table().Striped()))
    {
        using (memberTable.BeginHeader())
        {
            <tr>
                <th>Avatar</th>
                <th>Id</th>
                <th>UserId</th>
                <th>Nickname</th>
                <th>Muted</th>
                <th>Auto-Kicked</th>
                <th>Actions</th>
            </tr>
        }
        using (var memberBody = memberTable.BeginBody())
        {
            foreach (var member in Model.Data.Data.Members)
            {
                using (var memberRow = memberBody.BeginRow())
                {
                    @memberRow.Cell(Html.AvatarImageForGroupMePicture(member.AvatarImage, member.Nickname))
                    @memberRow.Cell(member.Id)
                    @memberRow.Cell(member.UserId)
                    @memberRow.Cell(member.Nickname)
                    @memberRow.Cell(Html.Bootstrap().Icon(member.Muted ? Icons.ok : Icons.remove))
                    @memberRow.Cell(Html.Bootstrap().Icon(member.AutoKicked ? Icons.ok : Icons.remove))
                    using (memberRow.BeginCell())
                    {
                        using (var buttonGroup = Html.Bootstrap().Begin(new ButtonGroup()))
                        {
                            @buttonGroup.ActionLinkButton(string.Empty, "DirectMessages", "Groups").AppendIcon(Icons.envelope).Tooltip(new Tooltip("Show Direct member messages").Placement("top")).RouteValues(new { groupId = Model.GroupId, memberId = member.UserId })
                            @(buttonGroup.ActionLinkButton(string.Empty, "RemoveMember", "Groups")
                                .HtmlAttributes(new { onclick = string.Format(CultureInfo.CurrentCulture, "return confirm('Do you really want to remove the member {0} / {1}?');", Html.Encode(member.Nickname), Html.Encode(member.Id)) })
                                .Tooltip(new Tooltip("Remove this member from the group.")
                                .Placement("top"))
                                .RouteValues(new { groupId = Model.GroupId, memberId = member.Id })
                                .AppendIcon(Icons.remove))
                        }
                    }
                }
            }
        }
    }
    
    using (FormBuilder<GroupDetailModel> form = Html.Bootstrap().Begin(new Form("AddMember").Type(FormType.Horizontal)))
    {
        <h3>Add member</h3>
        @Html.HiddenFor(m => m.GroupId)
        @form.ControlGroup().TextBoxFor(m => m.AddMember.Nickname).Label().LabelText("Nickname")
        @form.ControlGroup().TextBoxFor(m => m.AddMember.Email).Label().LabelText("Email")
        @form.ControlGroup().TextBoxFor(m => m.AddMember.PhoneNumber).Label().LabelText("Phone number")
        @form.ControlGroup().TextBoxFor(m => m.AddMember.UserId).Label().LabelText("User ID")
        @form.ControlGroup().CustomControls(Html.Bootstrap().SubmitButton())
    }
}